package com.bcu.cn.onlineexamsystem.studentmanage.mapper;

import com.bcu.cn.onlineexamsystem.studentmanage.entity.StudentAnswer;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface StudentAnswerMapper {
    
    @Insert("INSERT INTO student_answers (record_id, question_id, answer) " +
            "VALUES (#{recordId}, #{questionId}, #{answer})")
    @Options(useGeneratedKeys = true, keyProperty = "answerId")
    int insert(StudentAnswer studentAnswer);
    
    @Insert("<script>" +
            "INSERT INTO student_answers (record_id, question_id, answer) VALUES " +
            "<foreach collection='answers' item='answer' separator=','>" +
            "(#{answer.recordId}, #{answer.questionId}, #{answer.answer})" +
            "</foreach>" +
            "</script>")
    int batchInsert(@Param("answers") List<StudentAnswer> answers);
    
    @Select("SELECT * FROM student_answers WHERE record_id = #{recordId}")
    List<StudentAnswer> findByRecordId(Long recordId);
    
    @Select("SELECT * FROM student_answers WHERE record_id = #{recordId} AND question_id = #{questionId}")
    StudentAnswer findByRecordAndQuestion(@Param("recordId") Long recordId, @Param("questionId") Long questionId);
    
    @Update("UPDATE student_answers SET answer = #{answer} " +
            "WHERE record_id = #{recordId} AND question_id = #{questionId}")
    int updateAnswer(StudentAnswer answer);
} 